package li.songe.gkd;

import androidx.activity.ComponentActivity;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.Utils;
import com.tencent.mmkv.MMKV;

import li.songe.gkd.R;
import li.songe.gkd.MainActivity;

@SuppressLint("CustomSplashScreen")
public class SplashActivity extends ComponentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isFirstStart();
    }

    /**
     * 判断是否是首次启动
     */
    public void isFirstStart() {
        SharedPreferences preferences = getSharedPreferences("NB_FIRST_START", 0);
        boolean isFirst = preferences.getBoolean("FIRST_START", true);
        if (isFirst) {// 第一次
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            //获取界面
            View view = LayoutInflater.from(this).inflate(R.layout.dialog_privacy_show, null);
            //将界面填充到AlertDiaLog容器并去除边框
            builder.setView(view);
            //取消点击外部消失弹窗
            builder.setCancelable(false);
            //创建AlertDiaLog
            builder.create();
            //AlertDiaLog显示
            AlertDialog dialog = builder.show();
            // 移除dialog的decorview背景色
            dialog.getWindow().getDecorView().setBackground(null);
            //初始化控件
            TextView but_ok = view.findViewById(R.id.but_ok);
            TextView but_return = view.findViewById(R.id.but_return);

            TextView YongHu = view.findViewById(R.id.YongHu);
            YongHu.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);//下划线
            YongHu.getPaint().setAntiAlias(true);//抗锯齿
            TextView YinSi = view.findViewById(R.id.YinSi);
            YinSi.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);//下划线
            YinSi.getPaint().setAntiAlias(true);//抗锯齿
            //同意按钮
            but_ok.setOnClickListener(view1 -> {
                preferences.edit().putBoolean("FIRST_START", false).apply();
                dialog.dismiss();
                ((App)getApplication()).appInit();
                startActivity(new Intent(SplashActivity.this, MainActivity.class));
//                finish();
            });

            //取消按钮
            but_return.setOnClickListener(view1 -> {
                dialog.dismiss();
                finish();
            });

            //用户协议
            YongHu.setOnClickListener(view1 -> {
                String url = "http://wuliaodebaozi2.top/轻启动APP隐私政策.htm"; // 要打开的链接地址
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(intent);

            });

            //隐私政策
            YinSi.setOnClickListener(view1 -> {
                String url = "http://wuliaodebaozi2.top/轻启动APP隐私政策.htm"; // 要打开的链接地址
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(intent);
            });
        } else {
            preferences.edit().putBoolean("FIRST_START", false).apply();
            Handler handler = new Handler();
            handler.post(() -> {
                startActivity(new Intent(SplashActivity.this, MainActivity.class));
                finish();
            });
        }
    }

    //拦截返回键事件
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
